package com.ruoyi.web.controller.module.points;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;

import com.ruoyi.system.domain.module.points.EstatePointsRecord;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.points.IEstatePointsRecordService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 积分使用记录Controller
 *
 * @author ruoyi
 * @date 2025-06-01
 */
@Validated
@Api(value = "积分使用记录控制器", tags = {"积分使用记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/pointsRecord")
public class EstatePointsRecordController extends BaseController {

	private final IEstatePointsRecordService pointsRecordService;
	private final IEstateService estateService;
	private final IPropertyService propertyService;
	private final IWxUserService wxUserService;

	/**
	 * 查询积分使用记录列表
	 */
	@ApiOperation("查询积分使用记录列表")
	@PreAuthorize("@ss.hasPermi('system:record:list')")
	@GetMapping("/list")
	public TableDataInfo<EstatePointsRecord> list(@Validated EstatePointsRecord estatePointsRecord) {
		SysUser sysUser = getUser();
		QueryWrapper<EstatePointsRecord> qw = new QueryWrapper<>();
		if (sysUser.getUserType() == 2) {
			qw.eq("property_id", sysUser.getPropertyId());
			if (sysUser.getEstateId() != null) {
				qw.eq("estate_id", sysUser.getEstateId());
			}
		} else if (sysUser.getUserType() == 3) {
			qw.eq("estate_id", sysUser.getEstateId());
		} else {
			if (estatePointsRecord.getPropertyId() != null) {
				qw.eq("property_id", estatePointsRecord.getPropertyId());
			}
			if (estatePointsRecord.getEstateId() != null) {
				qw.eq("estate_id", estatePointsRecord.getEstateId());
			}
		}
		TableDataInfo<EstatePointsRecord> td = PageUtils.buildDataInfo(pointsRecordService.page(PageUtils.buildPage(), qw));
		List<EstatePointsRecord> list = td.getRows();
		for (EstatePointsRecord list1 : list) {
			if (list1.getPropertyId() != null) {
				list1.setPropertyName(propertyService.findById(list1.getPropertyId()).getPropertyName());
			}
			if (list1.getEstateId() != null) {
				list1.setEstateName(estateService.findById(list1.getEstateId()).getEstateName());
			}
			if (list1.getWxUserId() != null) {
				WxUser wxUser = wxUserService.findById(list1.getWxUserId());
				list1.setWxUserName(wxUser.getNickName());
				list1.setFaceUrl(wxUser.getFaceUrl());
			}
		}
		return td;
	}

	/**
	 * 导出积分使用记录列表
	 */
	@ApiOperation("导出积分使用记录列表")
	@PreAuthorize("@ss.hasPermi('system:record:export')")
	@Log(title = "积分使用记录", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<EstatePointsRecord> export(@Validated EstatePointsRecord estatePointsRecord) {
		List<EstatePointsRecord> list = pointsRecordService.list();
		ExcelUtil<EstatePointsRecord> util = new ExcelUtil<EstatePointsRecord>(EstatePointsRecord.class);
		return util.exportExcel(list, "积分使用记录");
	}

	/**
	 * 获取积分使用记录详细信息
	 */
	@ApiOperation("获取积分使用记录详细信息")
	@PreAuthorize("@ss.hasPermi('system:record:query')")
	@GetMapping("/{id}")
	public AjaxResult<EstatePointsRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
		EstatePointsRecord estatePointsRecord = pointsRecordService.getById(id);
		return AjaxResult.success(estatePointsRecord);
	}

	/**
	 * 新增积分使用记录
	 */
	@ApiOperation("新增积分使用记录")
	@PreAuthorize("@ss.hasPermi('system:record:add')")
	@Log(title = "积分使用记录", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstatePointsRecord estatePointsRecord) {
		return toAjax(pointsRecordService.save(estatePointsRecord) ? 1 : 0);
	}

	/**
	 * 修改积分使用记录
	 */
	@ApiOperation("修改积分使用记录")
	@PreAuthorize("@ss.hasPermi('system:record:edit')")
	@Log(title = "积分使用记录", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstatePointsRecord estatePointsRecord) {
		return toAjax(pointsRecordService.updateById(estatePointsRecord) ? 1 : 0);
	}

	/**
	 * 删除积分使用记录
	 */
	@ApiOperation("删除积分使用记录")
	@PreAuthorize("@ss.hasPermi('system:record:remove')")
	@Log(title = "积分使用记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
		return toAjax(pointsRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
	}
}
